#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int main(int argc, const char *argv[])
{
	int sock = socket(AF_INET, SOCK_STREAM, 0);
	int ret = 0;

	struct sockaddr_in peeraddr;
	peeraddr.sin_family = AF_INET;
	peeraddr.sin_addr.s_addr = inet_addr("192.168.200.8");
	peeraddr.sin_port = htons(22);

	printf("sock = %d\n", sock);
	ret = connect(sock, (struct sockaddr*)&peeraddr, sizeof(peeraddr));
	printf("connect ret = %d\n", ret);
	if(ret < 0) {
		fprintf(stderr, "connect err:%s\n", strerror(errno));
		exit(-1);
	}

	struct sockaddr_in hostaddr;
	socklen_t hostlen = sizeof(hostaddr);
	ret = getsockname(sock, (struct sockaddr*)&hostaddr, &hostlen);
	printf("ret = %d, host addr = %s\n", ret, inet_ntoa(hostaddr.sin_addr));
	if(ret < 0) {
		fprintf(stderr, "getsockname err:%s\n", strerror(errno));
	}

	close(sock);
	exit(0);
}
